<?php


/**
 * 订单接口
 * 2017年6月9日10:36:59
 */

class Goods extends Web_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Goods_model','Goods_model');
	}

		
	/**
     * [getDetail 根据id获取详情数据]
     * @return [json] [10001=参数异常 10002=订餐不存在 0=成功]
     */
   
    public function getDetail()
    {
        $cartData = $this->input->post('cartData');

        if(!isset($cartData)){
            echo json_encode(array('result'=>10001,'errmsg'=>'参数异常'));return;
        }
        $column = array_column($cartData, 'quantity','id');

        $ids = array_keys($column);
        $data = $this->Goods_model->getGoodsListByIds($ids);
        $datas['effect'] = array();
        $datas['noeffect'] = array();
        foreach($data as $key => $value){
            $res = $this->JsonsToArray($value['banner_img']);
            $data[$key]['img_path'] = isset($res['img_path'][0])?$res['img_path'][0]:'';
            $data[$key]['img_name'] = isset($res['img_name'][0])?$res['img_name'][0]:'';
            $data[$key]['price'] = sprintf("%.2f",$value['price']);
            $data[$key]['amount'] = $column[$value['goods_id']];
            $data[$key]['total_price'] = number_format($data[$key]['amount']*$value['price'], 2);
            unset($data[$key]['banner_img']);
            if($data[$key]['status'] == 1){
                $datas['noeffect'][] = array_merge($data[$key],$datas['noeffect']);
            }else{
                $datas['effect'][] = array_merge($data[$key],$datas['effect']);
            }
        } 
       
        echo json_encode(array('result'=>0,'errmsg'=>'ok','data'=>$datas));
    }

    public function JsonSToArray($json){
        $res = json_decode($json,true);
        $data = array();
        if(!empty($res)){
            foreach($res as $key => $value){                
                $data['img_path'][] = $key;
                $data['img_name'][] = $value;
            }
        }

        return $data;
    }

}